home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / djgpp / contrib / pdcurs22 / src / portable / getstr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-26  |  7.4 KB  |  282 lines

  1. /*
  2. ***************************************************************************
  3. * This file comprises part of PDCurses. PDCurses is Public Domain software.
  4. * You may use this code for whatever purposes you desire. This software
  5. * is provided AS IS with NO WARRANTY whatsoever.
  6. * Should this software be used in another application, an acknowledgement
  7. * that PDCurses code is used would be appreciated, but is not mandatory.
  8. *
  9. * Any changes which you make to this software which may improve or enhance
  10. * it, should be forwarded to the current maintainer for the benefit of 
  11. * other users.
  12. *
  13. * The only restriction placed on this code is that no distribution of
  14. * modified PDCurses code be made under the PDCurses name, by anyone
  15. * other than the current maintainer.
  16. * See the file maintain.er for details of the current maintainer.
  17. ***************************************************************************
  18. */
  19. #define    CURSES_LIBRARY    1
  20. #include <curses.h>
  21.  
  22. /* undefine any macros for functions defined in this module */
  23. #undef    getstr
  24. #undef    wgetstr
  25. #undef    mvgetstr
  26. #undef    mvwgetstr
  27. #undef    ungetstr
  28. #undef    wgetnstr
  29.  
  30. /* undefine any macros for functions called by this module if in debug mode */
  31. #ifdef PDCDEBUG
  32. #  undef    wrefresh
  33. #  undef    waddch
  34. #  undef    wgetch
  35. #  undef    cbreak
  36. #  undef    move
  37. #  undef    wmove
  38. #endif
  39.  
  40. #ifdef PDCDEBUG
  41. char *rcsid_getstr  = "$Id$";
  42. #endif
  43.  
  44. /*man-start*********************************************************************
  45.  
  46.   Name:                                                        getstr
  47.  
  48.   Synopsis:
  49.       int getstr(char *str);
  50.       int wgetstr(WINDOW *win, char *str);
  51.       int mvgetstr(int y, int x, char *str);
  52.       int mvwgetstr(WINDOW *win, int y, int x, char *str);
  53.       int wgetnstr(WINDOW *win, char *str, int ch);
  54.  
  55.   X/Open Description:
  56.      The effect of getstr() is as though a series of calls to getch()
  57.      were made, until a newline or carriage return is received. The
  58.      resulting value is placed in the area pointed to by *str. The user's
  59.      erase and kill characters are interpreted, as well as any special
  60.      keys; such as function keys.
  61.  
  62.      With wgetnstr(), a series of characters are read until a newline 
  63.      or carriage return is received.  The resulting value is placed 
  64.      in the area pointed to by the character pointer str.  The user's 
  65.      erase and kill characters are interpreted.  This differs from 
  66.      wgetstr() in that the number of characters read is limited by a passed
  67.      argument.
  68.  
  69.      NOTE: getstr(), mvgetstr() and mvwgetstr() are implemented as macros.
  70.  
  71.      WARNING:  There is no way to know how long the buffer passed to
  72.          wgetstr() is, so it is possible to overwrite wrong
  73.          memory or code!! This is the reason for the wgetnstr()
  74.          function :-)
  75.  
  76.   X/Open Return Value:
  77.      This functions return ERR on failure or any other value on success.
  78.  
  79.   X/Open Errors:
  80.      No errors are defined for this function.
  81.  
  82.   Portability                             X/Open    BSD    SYS V
  83.                                           Dec '88
  84.       getstr                                Y        Y       Y
  85.       wgtdch                                Y        Y       Y
  86.       mvgetstr                              Y        Y       Y
  87.       mvwgetstr                             Y        Y       Y
  88.       wgetnstr                              -        -      4.0
  89.  
  90. **man-end**********************************************************************/
  91.  
  92. #define MAXLINE 255
  93.  
  94. /***********************************************************************/
  95. int    getstr(char *str)
  96. /***********************************************************************/
  97. {
  98. #ifdef PDCDEBUG
  99.     if (trace_on) PDC_debug("getstr() - called\n");
  100. #endif
  101.  
  102.     if (stdscr == (WINDOW *)NULL)
  103.         return (ERR);
  104.     return(wgetnstr(stdscr,str,MAXLINE));
  105. }
  106. /***********************************************************************/
  107. int    wgetstr(WINDOW *win, char *str)
  108. /***********************************************************************/
  109. {
  110. #ifdef PDCDEBUG
  111.     if (trace_on) PDC_debug("wgetstr() - called\n");
  112. #endif
  113.  
  114.     if (win == (WINDOW *)NULL)
  115.         return (ERR);
  116.  
  117.     return(wgetnstr(win,str,MAXLINE));
  118. }
  119. /***********************************************************************/
  120. int    mvgetstr(int y, int x, char *str)
  121. /***********************************************************************/
  122. {
  123. #ifdef PDCDEBUG
  124.     if (trace_on) PDC_debug("mvgetstr() - called\n");
  125. #endif
  126.  
  127.     if (stdscr == (WINDOW *)NULL)
  128.         return (ERR);
  129.     if (move(y,x) == ERR)
  130.         return (ERR);
  131.     return(wgetnstr(stdscr,str,MAXLINE));
  132. }
  133. /***********************************************************************/
  134. int    mvwgetstr(WINDOW *win, int y, int x, char *str)
  135. /***********************************************************************/
  136. {
  137. #ifdef PDCDEBUG
  138.     if (trace_on) PDC_debug("mvwgetstr() - called\n");
  139. #endif
  140.  
  141.     if (win == (WINDOW *)NULL)
  142.         return (ERR);
  143.     if (wmove(win,y,x) == ERR)
  144.         return (ERR);
  145.     return(wgetnstr(win,str,MAXLINE));
  146. }
  147. /***********************************************************************/
  148. int    wgetnstr(WINDOW *win, char *str, int n)
  149. /***********************************************************************/
  150. {
  151.     int    ch, i, num,chars=0;
  152.     int    t = win->_tabsize;
  153.     int    x = win->_curx;
  154.     char*    p = str;
  155.     bool    stop = FALSE;
  156.     bool    oldecho;
  157.     bool    oldcbreak;
  158.     bool    oldnodelay;
  159.  
  160. #ifdef PDCDEBUG
  161.     if (trace_on) PDC_debug("wgetnstr() - called\n");
  162. #endif
  163.  
  164.     if (win == (WINDOW *)NULL)
  165.         return (ERR);
  166.  
  167. #ifdef UNIX
  168. /*
  169.  * this code is very dodgy
  170.  *
  171.     wrefresh(win);
  172.  
  173.     while ((*str = wgetch(win)) != ERR && *str != '\n')
  174.         ;
  175.     if (*str == ERR) {
  176.         *str = '\0';
  177.         waddstr(win,p);
  178.         return ERR;
  179.     }
  180.     *str = '\0';
  181.     waddstr(win,p);
  182.     return OK;
  183. */
  184. #else
  185.     oldcbreak = _cursvar.cbreak;    /* remember states     */
  186.     oldecho = _cursvar.echo;
  187.     oldnodelay = win->_nodelay;
  188.  
  189.     _cursvar.echo = FALSE;        /* we do echo ourselves    */
  190.     cbreak();        /* ensure each key is returned immediately */
  191.     win->_nodelay = FALSE;        /* don't return    -1     */
  192.  
  193.     wrefresh (win);
  194.  
  195.     while (!stop)
  196.     {
  197.     switch (ch = wgetch (win) & A_CHARTEXT)
  198.         {
  199.         case '\t':
  200.             ch = ' ';
  201.             num = t - (win->_curx - x)%t;
  202.             for (i=0; i<num; i++)
  203.                 {
  204.                 if (oldecho) 
  205.                     waddch (win, ch);
  206.                 *p++ = ch;
  207.                 }
  208.             chars += num;
  209.             break;
  210.  
  211.         case _ECHAR:  /* CTRL-H */     /* Delete character */
  212.             if (p > str)
  213.                 {
  214.                 if (oldecho) 
  215.                     waddstr (win, "\b \b");
  216.                 ch = *--p;
  217.                 if ((ch < ' ') && (oldecho))
  218.                     waddstr (win, "\b \b");
  219.                 }
  220.             chars--;
  221.             break;
  222.  
  223.         case _DLCHAR:  /* CTRL-U */     /* Delete line    */
  224.             while (p > str)
  225.                 {
  226.                 if (oldecho) 
  227.                     waddstr (win, "\b \b");
  228.                 ch = *--p;
  229.                 if ((ch < ' ') && (oldecho))
  230.                     waddstr (win, "\b \b");
  231.                 }
  232.             chars = 0;
  233.             break;
  234.  
  235.         case _DWCHAR: /* CTRL-W */    /* Delete word */
  236.             while ((p > str) && (*(p-1) == ' '))
  237.                 {
  238.                 if (oldecho) 
  239.                     waddstr (win, "\b \b");
  240.                 --p; /* remove space */
  241.                 chars--;
  242.                 }
  243.             while ((p > str) && (*(p-1) != ' '))
  244.                 {
  245.                 if (oldecho) 
  246.                     waddstr (win, "\b \b");
  247.                 ch = *--p;
  248.                 if ((ch < ' ') && (oldecho))
  249.                     waddstr (win, "\b \b");
  250.                 chars--;
  251.                 }
  252.             break;
  253.  
  254.         case '\n':
  255.         case '\r':
  256.             stop = TRUE;
  257.             if (oldecho) 
  258.                 waddch (win, '\n');
  259.             break;
  260.  
  261.         default:
  262.             *p++ = ch;
  263.             if (oldecho) 
  264.                 waddch (win, ch);
  265.             chars ++;
  266.             break;
  267.         }
  268.     wrefresh (win);
  269.     if (chars >= n)
  270.         break;
  271.     }
  272.     *p = '\0';
  273.  
  274.     _cursvar.echo = oldecho;    /* restore old settings    */
  275.     _cursvar.cbreak = oldcbreak;
  276.     win->_nodelay = oldnodelay;
  277.  
  278.     return (OK);
  279. #endif
  280. }
  281.